package com.movie.act;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.movie.bean.MovieTicket;
import com.movie.bean.MovieTicketList;
import com.movie.bean.PointList;
import com.movie.util.ScreenInfo;

/**
 * 09-影片排期购 票
 * 
 * 
 */
public class BuyTicketAct extends BaseActivity {

	private Context mContext = BuyTicketAct.this;
	private PointList mPointList = new PointList();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buyticketact);
		mPointList.grid = ScreenInfo.getSeatGridWidth();
		getData();
		initZoneSubSlected();
		printTable(mPointList);
		initMovieTickets();
		initBackTitleBar();
		onclick();
		initThreeLine();
		// showSeatPositions(10, 12);
	}

	private LinearLayout leftl = null;
	private TextView leftl_textview = null;
	private LinearLayout rgithl = null;
	private TextView rgithl_textview = null;

	private void initZoneSubSlected() {
	
		leftl = (LinearLayout) findViewById(R.id.leftl);
		leftl_textview = (TextView) findViewById(R.id.leftl_textview);
		rgithl = (LinearLayout) findViewById(R.id.rgithl);
		rgithl_textview = (TextView) findViewById(R.id.rgithl_textview);
		leftl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				zoneSppiner.show();
			}
		});

		rgithl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cinemasppiner.show();
			}
		});
		openZoneSlect(zones);
		openCinemaSelect(subzone.get(zones[0]));
	}

	/**
	 * 对话框
	 */
	private AlertDialog zoneSppiner = null;

	private void openZoneSlect(final String[] zones) {
		zoneSppiner = new AlertDialog.Builder(mContext).setTitle("地区")
				.setItems(zones, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						temp = subzone.get(zones[which]);
						leftl_textview.setText(zones[which]);
						rgithl_textview.setText(temp.get(0));
						openCinemaSelect(temp);
						zoneSppiner.dismiss();
					}

				}).create();
	}

	/**
	 * 对话框
	 */
	private AlertDialog cinemasppiner = null;

	private void openCinemaSelect(ArrayList<String> temp) {

		final String[] items = new String[temp.size()];
		temp.toArray(items);
		cinemasppiner=	new AlertDialog.Builder(mContext).setTitle("影院").setItems(items,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						rgithl_textview.setText(items[which]);
						cinemasppiner.dismiss();
					}

				}).create();
	}

	private String[] dateArray = new String[] { "1月12日(今)", "1月13日(明)",
			"1月14日(后)" };
	private HashMap<String, ArrayList<MovieTicket>> data = new HashMap<String, ArrayList<MovieTicket>>();
	private String[] zones = null;
	private ArrayList<String> temp = null;
	private HashMap<String, ArrayList<String>> subzone = null;

	private void getData() {
		zones = new String[] { "福田区", "福田区2" };
		if (subzone == null) {
			subzone = new HashMap<String, ArrayList<String>>();
			ArrayList<String> sub1 = new ArrayList<String>();
			sub1.add("百老汇影院");
			sub1.add("保利影院");

			ArrayList<String> sub2 = new ArrayList<String>();
			sub2.add("百老汇影院1");
			sub2.add("保利影院1");

			subzone.put(zones[0], sub1);
			subzone.put(zones[1], sub2);
		}
		mMovieTicketList = new MovieTicketList();
		MovieTicket mMovieTicket = null;
		ArrayList<MovieTicket> mArrayList1 = new ArrayList<MovieTicket>();

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "8:10";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList1.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "8:20";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList1.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "8:30";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList1.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "8:50";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList1.add(mMovieTicket);

		data.put("0", mArrayList1);

		ArrayList<MovieTicket> mArrayList2 = new ArrayList<MovieTicket>();

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "12:10";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList2.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "13:20";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList2.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "14:30";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList2.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "15:50";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList2.add(mMovieTicket);

		data.put("1", mArrayList2);

		ArrayList<MovieTicket> mArrayList3 = new ArrayList<MovieTicket>();

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "19:10";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList3.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "19:20";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList3.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "20:30";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList3.add(mMovieTicket);

		mMovieTicket = new MovieTicket();
		mMovieTicket.time = "21:50";
		mMovieTicket.type = "国语";
		mMovieTicket.price = 10 + "元";
		mMovieTicket.movitype = "3D";
		mArrayList3.add(mMovieTicket);

		data.put("2", mArrayList3);

	}

	private LinearLayout line1 = null;
	private LinearLayout line2 = null;
	private LinearLayout line3 = null;

	private LinearLayout highLight = null;
	private int[] backgound = new int[] { R.drawable.noon_slectedbg,
			R.drawable.noon_unslectedbg };

	private void initThreeLine() {
		line1 = (LinearLayout) findViewById(R.id.line1);
		line2 = (LinearLayout) findViewById(R.id.line2);
		line3 = (LinearLayout) findViewById(R.id.line3);
		highLight = line1;
		line1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				highLight.setBackgroundResource(backgound[1]);
				line1.setBackgroundResource(backgound[0]);
				highLight = line1;
				selected = -1;
				ArrayList<MovieTicket> data1 = data.get("0");
				mMovieTicketList.mMovieTickets.clear();
				mMovieTicketList.mMovieTickets
						.addAll(new ArrayList<MovieTicket>(data1));
				initMovieTickets();
			}
		});

		line2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				highLight.setBackgroundResource(backgound[1]);
				line2.setBackgroundResource(backgound[0]);
				highLight = line2;
				selected = -1;
				mMovieTicketListAdapter.notifyDataSetChanged();

				ArrayList<MovieTicket> data1 = data.get("1");
				mMovieTicketList.mMovieTickets.clear();
				mMovieTicketList.mMovieTickets
						.addAll(new ArrayList<MovieTicket>(data1));
				initMovieTickets();
			}
		});

		line3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				highLight.setBackgroundResource(backgound[1]);
				line3.setBackgroundResource(backgound[0]);
				highLight = line3;
				selected = -1;
				mMovieTicketListAdapter.notifyDataSetChanged();

				ArrayList<MovieTicket> data1 = data.get("2");
				mMovieTicketList.mMovieTickets.clear();
				mMovieTicketList.mMovieTickets
						.addAll(new ArrayList<MovieTicket>(data1));
				initMovieTickets();
			}
		});

	}

	private ImageView yingyuanxiangqing = null;
	// private ImageView duihuangquan = null;

	private TextView goseatselected = null;

	private void onclick() {
		goseatselected = (TextView) findViewById(R.id.goseatselected);
		goseatselected.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(mContext, SeatSelectedAct.class);
				mContext.startActivity(intent);
			}
		});
		yingyuanxiangqing = (ImageView) findViewById(R.id.yingyuanxiangqing);
		yingyuanxiangqing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, CinemaDesAct.class);
				mContext.startActivity(intent);
			}
		});
		// duihuangquan = (ImageView) findViewById(R.id.duihuangquan);
		// duihuangquan.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// }
		// });
		onDateClick();
	}

	private int dateIndex = 0;

	private ImageView jian = null;
	private ImageView jia = null;
	private TextView zhangshu = null;

	private void onDateClick() {

		jian = (ImageView) findViewById(R.id.jian);
		jia = (ImageView) findViewById(R.id.jia);
		zhangshu = (TextView) findViewById(R.id.zhangshu);

		jia.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addLogic();
			}

		});
		;
		jian.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				decreaseLogic();
			}

		});
		setZhangshu();
	}

	private void decreaseLogic() {
		if (dateIndex > 0) {
			dateIndex -= 1;
			zhangshu.setText(dateArray[dateIndex] + "");
			if (dateIndex == 0) {
				jia.setVisibility(View.VISIBLE);
				jian.setVisibility(View.GONE);
			} else if (dateIndex < 2) {
				jia.setVisibility(View.VISIBLE);
			}
		}
	}

	private void setZhangshu() {
		if (dateIndex == 0) {
			jia.setVisibility(View.VISIBLE);
			jian.setVisibility(View.GONE);
		} else if (dateIndex == 2) {
			jia.setVisibility(View.GONE);
			jian.setVisibility(View.VISIBLE);
		}
	}

	private void addLogic() {
		if (dateIndex < 2) {
			dateIndex += 1;
			zhangshu.setText(dateArray[dateIndex]);
			if (dateIndex == 2) {
				jia.setVisibility(View.GONE);
				jian.setVisibility(View.VISIBLE);
			} else if (dateIndex > 0) {
				jian.setVisibility(View.VISIBLE);
			}
			// jian.setBackgroundResource(backgrounds[1]);
			// jian.setEnabled(false);
		}
	}

	// -------------------电影票----------START
	private void initMovieTickets() {
		if (movie_listview != null) {
			mMovieTicketListAdapter.notifyDataSetChanged();
			return;
		}
		movie_listview = (ListView) findViewById(R.id.movie_listview);

		mMovieTicketListAdapter = new MovieTicketListAdapter();
		movie_listview.setAdapter(mMovieTicketListAdapter);
		// movie_listview.setOnItemSelectedListener(new OnItemSelectedListener()
		// {
		//
		// public void onItemSelected(AdapterView<?> mAdapterView,
		// View parent, int position, long id) {
		// selected = position;
		// mMovieTicketListAdapter.notifyDataSetChanged();
		// }
		//
		// public void onNothingSelected(AdapterView<?> arg0) {
		// }
		// });
		movie_listview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> mAdapterView, View parent,
					int position, long id) {
				selected = position;
				mMovieTicketListAdapter.notifyDataSetChanged();
			}
		});

	}

	private int selected = -1;
	private boolean ismListViewHidden = true;
	private ListView movie_listview = null;
	private MovieTicketListAdapter mMovieTicketListAdapter = null;
	private MovieTicketList mMovieTicketList = null;

	public class MovieTicketListAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public MovieTicketListAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return mMovieTicketList.mMovieTickets.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderListViewItme holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.movie_ticket_list_item, null);
				holder = new ViewHolderListViewItme();
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.type = (TextView) convertView.findViewById(R.id.type);
				holder.price = (TextView) convertView.findViewById(R.id.price);
				holder.movitype = (TextView) convertView
						.findViewById(R.id.movitype);
				holder.listl = (LinearLayout) convertView
						.findViewById(R.id.listl);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolderListViewItme) convertView.getTag();
				holder.listl.setBackgroundResource(itembg[1]);
			}
			MovieTicket bean = mMovieTicketList.mMovieTickets.get(position);
			holder.time.setText(bean.time);
			holder.type.setText(bean.type);
			holder.price.setText(bean.price);
			holder.movitype.setText(bean.movitype);

			if (selected != -1) {
				if (selected == position) {
					holder.listl.setBackgroundResource(itembg[0]);
				} else {
					holder.listl.setBackgroundResource(itembg[1]);
				}
			}

			return convertView;
		}
	}

	private int[] itembg = new int[] { R.drawable.noon_slectedbg,
			R.drawable.noon_unslectedbg };

	private static class ViewHolderListViewItme {
		public TextView time = null;
		public TextView type = null;
		public TextView price = null;
		public TextView movitype = null;
		public LinearLayout listl = null;

	}

	// -------------------电影票-----------END

	// -------------------座位-----------START
	private TextView tikcetseatNum = null;
	private TextView[] tikcetseatNumTemp = null;
	private ImageView tikcetseatSeat = null;
	private LinearLayout seat_lable = null;
	private LinearLayout seat_rows = null;
	private LinearLayout seat_row_L = null;

	// 表头动态容器

	// 表内容动太容器
	private LinearLayout seat_lable_pl = null;
	private LinearLayout seat_row_pl = null;

}
